﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.Objects;
using Autocentro.Entities;

namespace Autocentro.Respository
{
    public class VentaRepository : MasterRepository, IVentaRepositor
    {
        public List<VENTA> GetFromVentasByCriterio(string criterio)
        {
            var query = from p in context.Venta
                        .Include("CLIENTE")
                        .Include("DETALLEVENTA.PRODUCTO")
                        select p;
            if (criterio != null && criterio != null && criterio != string.Empty)
            {
                query = query.Where(p => p.Cliente.RazonSocial_Nombre.ToUpper().Contains(criterio.ToUpper())
                    || p.Cliente.Ruc_DNI.Contains(criterio));
            }
            ObjectContext.Refresh(RefreshMode.StoreWins,query);

            return query.ToList();
        }

        public VENTA GetFromVentaById(int id)
        {
            var query = from p in context.Venta
                        //.Include("CLIENTE")
                        .Include("DETALLEVENTA.PRODUCTO")
                        where p.Id.Equals(id)
                        select p;

            ObjectContext.Refresh(RefreshMode.StoreWins, query);
            return query.SingleOrDefault();
        }

        public void AddVenta(VENTA venta)
        {
            foreach (var item in venta.DetalleVenta)
            {
                context.Entry(item.Producto).State = EntityState.Unchanged;
                context.Entry(item).State = EntityState.Added;  
            }

            context.Entry(venta.Cliente).State = EntityState.Unchanged;
            context.Venta.Add(venta);
            context.SaveChanges();
        }

        public void UpdateVenta(VENTA venta)
        {
            var estadosDetalle = context.ChangeTracker.Entries<DETALLEVENTA>();

            context.Entry(venta.Cliente).State = EntityState.Unchanged;
            context.Entry(venta).State = EntityState.Modified;

            foreach (var item in venta.DetalleVenta)
            {
                item.Producto = null;

                context.Entry(item).State = item.Item ==
                    Int32.Parse("0") ? EntityState.Added : EntityState.Modified;

                item.IdProducto = item.IdProducto;
            }

            context.SaveChanges();

        }

        public void CancelarVenta(int id)
        {
            var existe = context.Venta.Find(id);

            if (existe!=null)
            {
                context.Entry(existe).State = EntityState.Modified;
                existe.Cancelada = true;
                context.SaveChanges();
            }
        }

        public List<VENTA> GetFromVentasbyFechasAndCliente(DateTime? fec1, DateTime? fec2, string cliente)
        {
            #region IF

            if (cliente != null && cliente != string.Empty && cliente != "")  // FECHAS - CLIENTE 
            {
                var query = from h in context.Venta.Include("CLIENTE").Include("DETALLEVENTA.PRODUCTO")
                            select h;

                if (fec1 != null && fec2 != null)//fechas y cliente
                {
                    query = from h in query
                            where (h.Fecha >= fec1.Value && h.Fecha <= fec2.Value) &&
                                    (h.Cliente.RazonSocial_Nombre.ToUpper().Contains(cliente.ToUpper()) ||
                                    h.Cliente.Ruc_DNI.ToUpper().Contains(cliente.ToUpper()))
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 != null && fec2 == null)//dia y cliente 
                {
                    query = from h in query
                            where
                            (h.Cliente.RazonSocial_Nombre.ToUpper().Contains(cliente.ToUpper()) ||
                            h.Cliente.Ruc_DNI.ToUpper().Contains(cliente.ToUpper())) && h.Fecha == fec1.Value
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 == null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            where h.Cliente.RazonSocial_Nombre.ToUpper().Contains(cliente.ToUpper()) ||
                            h.Cliente.Ruc_DNI.ToUpper().Contains(cliente.ToUpper())
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                else //retornar todos
                {
                    query = from h in query
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                return query.Take(500).ToList();
            }

            #endregion

            #region ELSE

            else //sin cliente
            {
                var query = from h in context.Venta.Include("CLIENTE").Include("DETALLEVENTA.PRODUCTO")
                            select h;

                if (fec1 != null && fec2 != null)//rango de fechas
                {
                    query = from h in query
                            where h.Fecha >= fec1.Value && h.Fecha <= fec2.Value
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 != null && fec2 == null)//solo dia
                {
                    query = from h in query
                            where
                            h.Fecha == fec1.Value
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                if (fec1 == null && fec2 == null)//solo cliente
                {
                    query = from h in query
                            select h;

                    ObjectContext.Refresh(RefreshMode.StoreWins, query);

                }
                return query.Take(500).ToList();
            }
            #endregion

        }
    }
}
